// pages/questionnairedone/questionnairedone.js
const APP = getApp();
import API from "../../api/Api.js"
import constants from "../../assets/constants.js"
import common from "../../assets/common.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userSurveyId: "",
    surveyName: "",
    QUESTION_TYPE: {},
    questions: [],
    answer: []
  },

  formSubmit(e) {
    let _that = this;
    console.log(e);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _that = this;
    _that.data.userSurveyId = options ? options.usersurveyid : "";
    _that.setData({
      surveyName: options.surveyname,
      QUESTION_TYPE: constants.QUESTION_TYPE
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let _that = this;
    _that.getQuestionnaire();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取问卷
   */
  getQuestionnaire() {
    let _that = this;
    _that.showLoading();
    API.questionnaire.getQuestionnaire({
      userId: APP.globalData.userVo.id,
      userSurveyId: _that.data.userSurveyId
    }).then(res => {
      if (!res) {
        _that.setData({
          questions: []
        });
        _that.hideLoading();
        return;
      }
      API.survey.getUserSurvey({
        userSurveyId: _that.data.userSurveyId
      }).then(ress => {
        if (!ress) {
          return;
        }
        let map = [];
        let answer = [];
        res.questions.forEach((v, k) => {
          map[v.id] = k;
        });
        ress.userSurveyResultVos.forEach(v => {
          answer[map[v.questionVo.id]] = v.answer;
        });
        _that.setData({
          answer,
          questions: res.questions ? res.questions : []
        });
        _that.hideLoading();
      })
    }).catch(() => {
      _that.hideLoading();
    });
  },

  /**
   * 显示加载
   */
  showLoading() {
    let _that = this;
    wx.showLoading({
      title: '加载中',
    });
  },

  /**
   * 隐藏加载
   */
  hideLoading() {
    let _that = this;
    wx.hideLoading();
  }
})